<?php
	 require_once ("db_connection.php");
	
	if( !isset($_POST['uponrequest'])){
		$requests = 1;
	}
	else{
		$requests = 0;
	}
	$id = "RA-" . uniqid();
	$tempcityid = "";
	$vehiclemodels  = '';
	
	$result = mysql_query("SELECT * FROM city WHERE cityname = '$_POST[cityname]'", $con);	
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$tempcityid = $row['cityid'] ;
	
	for($i=0;$i<count($_POST["vehiclemodel"]);$i++)
	{	
		$vehiclemodels = $vehiclemodels . $_POST["vehiclemodel"][$i] . ',';
	}
	
	$query = "INSERT INTO ralimorates(id, cityid, cityname, companyname, vehicletype, vehiclemodel, capacity, cancellationpolicy, availability)
		VALUES('$id', '$tempcityid', '$_POST[cityname]', '$_POST[companyname]', '$_POST[vehicletype]', '$vehiclemodels' ,'$_POST[capacity]', '$_POST[cancellation]', '$requests')";
	
	if(!mysql_query($query, $con)) {
		die('Error: '. mysql_error());
	}

	if($requests == 0){
		$ids = "RL-" . uniqid();
		$dest = '';
		$hr = '';
		$type = 'uponrequest';
		
		$query = "INSERT INTO ralimoratesdetail(id, vehicletype, ralimorateid, ratetype, destination, minimumhr, currency, rate)
			VALUES('$ids', '$_POST[vehicletype]', '$id', '$type' ,'$dest', '$hr' ,'', '')";

		if(!mysql_query($query, $con)) {
			die('Error: '. mysql_error());
		}
	}

	
	$data = array("Saved!", $id, $_POST['vehicletype']);
	
	mysql_close($con);
	
	echo json_encode($data);
	flush();
?>